package com.glc.hms.config;


import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.extension.plugins.OptimisticLockerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.glc.hms.common.exception.ServiceException;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * @author Santa
 * @since 2019/11/14
 */
@Configuration
@MapperScan(basePackages = {"com.glc.hms.*.dao"})
public class MybatisPlusConfig {



    /**
     * xml文件的位置
     */
    //public static final String MAPPING_XML_CLASSPATH = "classpath:com/glc/hms/system/**/mapping/*.xml";
    public static final String MAPPING_XML_CLASSPATH = "classpath:/mapper/*Mapper.xml";



    @Bean
    public SqlSessionFactory sqlSessionFactoryPrimary(DataSource dataSource) {
        return createSqlSessionFactory(dataSource);
    }

    /**
     * 创建数据源
     */
    private SqlSessionFactory createSqlSessionFactory(DataSource dataSource) {
        try {
            MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPING_XML_CLASSPATH));
            bean.setVfs(SpringBootVFS.class);
            bean.setPlugins(new Interceptor[]{
                        paginationInterceptor(),
                        optimisticLockerInterceptor(),
//                        performanceInterceptor(),

                });
            return bean.getObject();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            throw new ServiceException(500, "SqlSessionFactory创建出错！");
        }
    }


    @Bean
    public DataSourceTransactionManager transactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }



    /**
     * SQL执行效率插件
     */
    @Bean
    //@Profile({"local","dev"})// 设置 dev test 环境开启
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }

    /**
     * mybatis-plus分页插件
     */
    @Bean
    public PaginationInterceptor paginationInterceptor() {
        return new PaginationInterceptor();
    }




    /**
     * 乐观锁mybatis插件
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor() {
        return new OptimisticLockerInterceptor();
    }


}























